<template>  
  <view v-if="cartList.cart.length!==0">   
    <my-address></my-address>

    <view class="cart-title">
      <uni-icons type="shop" size="18"></uni-icons>
      <text class="cart-title-text">购物车</text>
    </view>
    <uni-swipe-action>
       <block v-for="(goods, i) in cartList.cart" :key="i">
      <uni-swipe-action-item :right-options="options" @click="swipeActionClickHandler(goods)">
        <my-goods @num-change="numberChangeHandler" :showNum="true" :goods="goods" :showRadio="true" @radio-change="radioChangeHandler"></my-goods>          
      </uni-swipe-action-item>
       </block>
    </uni-swipe-action>
   
  <my-settle></my-settle>
    
  </view>
  
  <view class="empty-cart" v-else>
      <image src="/static/cart_empty@2x.png" class="empty-img"></image>
      <text class="tip-text">空空如也~</text>
    </view>
</template>

<script setup>
  import {moduleCart}  from '../../store/cart.js'
  import {onLoad,onReachBottom,onPullDownRefresh,onShow} from '@dcloudio/uni-app'
import { reactive } from "vue";

  const options=reactive([{
    text:'删除',
    style:{
      backgroundColor:'#C00000'
    }
  }])
  const cartList=moduleCart()
  const setBadge=()=>{
    uni.setTabBarBadge({
      index:2,
      text:cartList.total+''
    })
  }
  
  onShow(()=>{
     setBadge()
  })
   
   const radioChangeHandler=(e)=>{
     cartList.updateGoodsState(e)
   }
   
   const numberChangeHandler=(e)=>{
    
    cartList.updateGoodsCount(e)
     setBadge()
}
    
    const swipeActionClickHandler=(goods)=>{
      uni.showModal({
      	title: '提示',
      	content: '是否删除该商品',
      	success: function (res) {
      		if (res.confirm) {
            uni.showToast({
            	title: '删除成功',
            	duration: 600,
              icon:"success"
            });
      			 cartList.removeGoodsById(goods.goods_id)
            setBadge()
      		} else if (res.cancel) {
      			return
      		}
      	}
      });
    }

</script>

<style lang="scss">
.cart-title {
  height: 40px;
  display: flex;
  align-items: center;
  font-size: 14px;
  padding-left: 5px;
  border-bottom: 1px solid #efefef;
  .cart-title-text {
    margin-left: 10px;
  }
}

.empty-cart {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 150px;

  .empty-img {
    width: 90px;
    height: 90px;
  }

  .tip-text {
    font-size: 12px;
    color: gray;
    margin-top: 15px;
  }
}
</style>
